<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/favicon.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.7.2/css/all.min.css" integrity="sha256-dABdfBfUoC8vJUBOwGVdm8L9qlMWaHTIfXt+7GnZCIo=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"blog.csgrandeur.cn","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.22.0","exturl":false,"sidebar":{"position":"left","width_expanded":320,"width_dual_column":240,"display":"post","padding":18,"offset":12},"hljswrap":true,"copycode":{"enable":true,"style":"default"},"fold":{"enable":false,"height":500},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"duration":200,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"top_n_per_article":1,"unescape":false,"preload":false,"trigger":"auto"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="函数与结构体 本章讲解C语言中函数与结构体的核心概念。函数通过定义、调用与参数传递实现代码复用与逻辑分离。递归用于解决分治问题，需注意栈溢出风险。结构体封装多种数据类型，定义自定义对象，支持成员函数与运算符重载，提升代码可读性与维护性。实例演示函数、递归与结构体的应用，帮助掌握模块化编程与复杂数据管理。">
<meta property="og:type" content="article">
<meta property="og:title" content="04.函数与结构体">
<meta property="og:url" content="http://blog.csgrandeur.cn/2025-03-06-04-%E5%87%BD%E6%95%B0%E4%B8%8E%E7%BB%93%E6%9E%84%E4%BD%93/index.html">
<meta property="og:site_name" content="CSGrandeur&#39;s Thinking">
<meta property="og:description" content="函数与结构体 本章讲解C语言中函数与结构体的核心概念。函数通过定义、调用与参数传递实现代码复用与逻辑分离。递归用于解决分治问题，需注意栈溢出风险。结构体封装多种数据类型，定义自定义对象，支持成员函数与运算符重载，提升代码可读性与维护性。实例演示函数、递归与结构体的应用，帮助掌握模块化编程与复杂数据管理。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://blog.csgrandeur.cn/2025-03-06-04-%E5%87%BD%E6%95%B0%E4%B8%8E%E7%BB%93%E6%9E%84%E4%BD%93/hanoi.gif">
<meta property="article:published_time" content="2025-03-06T10:26:57.000Z">
<meta property="article:modified_time" content="2025-03-06T10:31:11.787Z">
<meta property="article:author" content="CSGrandeur">
<meta property="article:tag" content="ACM">
<meta property="article:tag" content="Algorithm">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://blog.csgrandeur.cn/2025-03-06-04-%E5%87%BD%E6%95%B0%E4%B8%8E%E7%BB%93%E6%9E%84%E4%BD%93/hanoi.gif">


<link rel="canonical" href="http://blog.csgrandeur.cn/2025-03-06-04-%E5%87%BD%E6%95%B0%E4%B8%8E%E7%BB%93%E6%9E%84%E4%BD%93/">


<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://blog.csgrandeur.cn/2025-03-06-04-%E5%87%BD%E6%95%B0%E4%B8%8E%E7%BB%93%E6%9E%84%E4%BD%93/","path":"2025-03-06-04-函数与结构体/","title":"04.函数与结构体"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>04.函数与结构体 | CSGrandeur's Thinking</title>
  

  <script src="/js/third-party/analytics/baidu-analytics.js"></script>
  <script async src="https://hm.baidu.com/hm.js?7958adf931092425a489778560129144"></script>







  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">CSGrandeur's Thinking</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">Cogito Ergo Sum</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
      <div class="search-header">
        <span class="search-icon">
          <i class="fa fa-search"></i>
        </span>
        <div class="search-input-container">
          <input autocomplete="off" autocapitalize="off" maxlength="80"
                placeholder="搜索..." spellcheck="false"
                type="search" class="search-input">
        </div>
        <span class="popup-btn-close" role="button">
          <i class="fa fa-times-circle"></i>
        </span>
      </div>
      <div class="search-result-container">
        <div class="search-result-icon">
          <i class="fa fa-spinner fa-pulse fa-5x"></i>
        </div>
      </div>
    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%87%BD%E6%95%B0%E4%B8%8E%E7%BB%93%E6%9E%84%E4%BD%93"><span class="nav-number">1.</span> <span class="nav-text">函数与结构体</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%87%BD%E6%95%B0%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="nav-number">1.1.</span> <span class="nav-text">函数基本概念</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E5%87%BD%E6%95%B0"><span class="nav-number">1.1.1.</span> <span class="nav-text">什么是函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="nav-number">1.1.2.</span> <span class="nav-text">函数的定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E8%B0%83%E7%94%A8%E4%B8%8E%E5%8F%82%E6%95%B0%E4%BC%A0%E9%80%92"><span class="nav-number">1.1.3.</span> <span class="nav-text">函数的调用与参数传递</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E6%84%8F%E4%B9%89"><span class="nav-number">1.1.4.</span> <span class="nav-text">函数的意义</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E9%80%92%E5%BD%92"><span class="nav-number">1.2.</span> <span class="nav-text">函数的递归</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%80%92%E5%BD%92%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="nav-number">1.2.1.</span> <span class="nav-text">递归的定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%80%92%E5%BD%92%E7%9A%84%E6%84%8F%E4%B9%89"><span class="nav-number">1.2.2.</span> <span class="nav-text">递归的意义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%80%92%E5%BD%92%E6%A0%88"><span class="nav-number">1.2.3.</span> <span class="nav-text">递归栈</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%93%E6%9E%84%E4%BD%93"><span class="nav-number">1.3.</span> <span class="nav-text">结构体</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%93%E6%9E%84%E4%BD%93%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="nav-number">1.3.1.</span> <span class="nav-text">结构体的概念</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%88%90%E5%91%98%E5%87%BD%E6%95%B0"><span class="nav-number">1.3.2.</span> <span class="nav-text">成员函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6%E7%9A%84%E9%87%8D%E8%BD%BD"><span class="nav-number">1.3.3.</span> <span class="nav-text">运算符的重载</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">CSGrandeur</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">72</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">22</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://blog.csgrandeur.cn/2025-03-06-04-%E5%87%BD%E6%95%B0%E4%B8%8E%E7%BB%93%E6%9E%84%E4%BD%93/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="CSGrandeur">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="CSGrandeur's Thinking">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="04.函数与结构体 | CSGrandeur's Thinking">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          04.函数与结构体
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2025-03-06 18:26:57 / 修改时间：18:31:11" itemprop="dateCreated datePublished" datetime="2025-03-06T18:26:57+08:00">2025-03-06</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/ACM/" itemprop="url" rel="index"><span itemprop="name">ACM</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/ACM/ACMCOURSE/" itemprop="url" rel="index"><span itemprop="name">ACMCOURSE</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><h1 id="函数与结构体">函数与结构体</h1>
<p>本章讲解C语言中函数与结构体的核心概念。函数通过定义、调用与参数传递实现代码复用与逻辑分离。递归用于解决分治问题，需注意栈溢出风险。结构体封装多种数据类型，定义自定义对象，支持成员函数与运算符重载，提升代码可读性与维护性。实例演示函数、递归与结构体的应用，帮助掌握模块化编程与复杂数据管理。</p>
<span id="more"></span>
<h2 id="函数基本概念">函数基本概念</h2>
<h3 id="什么是函数">什么是函数</h3>
<ul>
<li>一段具有特定功能的代码块</li>
<li>可以执行某项特定任务并返回结果</li>
</ul>
<p>例如输入<code>a</code>与<code>b</code>求<code>a+b</code>这件事，不使用函数：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d%d&quot;</span>, &amp;a, &amp;b);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, a + b);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>使用函数：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">Add</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d%d&quot;</span>, &amp;a, &amp;b);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, <span class="built_in">Add</span>(a, b));</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="函数的定义">函数的定义</h3>
<p>函数的定义：</p>
<ol type="1">
<li>返回值类型：定义返回类型，<code>void</code>则表示不返回内容；</li>
<li>函数名：用该名字调用函数；</li>
<li>参数类型： <span
class="math inline">\(0\)</span>个或多个参数，参数之间用“<code>,</code>”隔开；</li>
<li>参数名：每个参数类型之后跟参数名，在函数里可以使用这些参数；</li>
<li>函数内逻辑：执行特定任务；</li>
<li>返回值：如果返回值类型不是<code>void</code>，则必须用<code>return</code>返回一个变量的值。</li>
</ol>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;返回值&gt; &lt;函数名&gt;(&lt;参数类型<span class="number">1</span>&gt; &lt;参数名<span class="number">1</span>&gt;, &lt;参数类型<span class="number">2</span>&gt; &lt;参数名<span class="number">2</span>&gt;, ...) &#123;</span><br><span class="line">    &lt;函数逻辑&gt;</span><br><span class="line">    <span class="keyword">return</span> &lt;返回值&gt;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>如果<code>&lt;返回值&gt;</code>不是<code>void</code>，则函数结尾必须<code>return &lt;返回值&gt;</code></p>
<p>无返回值的函数示例：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> <span class="title">Hello</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;Hello, World!&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="built_in">Hello</span>();</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>注意事项：</p>
<ul>
<li>推荐将自定义函数写在<code>int main</code>的上面，否则编译器在执行<code>main()</code>的时候不知道这个函数；</li>
<li>函数的名称和变量名都是大小写敏感的，定义<code>Add()</code>就不能用<code>add()</code>去调用；</li>
</ul>
<h3 id="函数的调用与参数传递">函数的调用与参数传递</h3>
<p>函数通过函数名调用，格式是
<code>&lt;函数名&gt; (&lt;参数1&gt;, &lt;参数2&gt;, ...);</code></p>
<ol type="1">
<li>没有返回值的函数，直接调用 <figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="built_in">Hello</span>();</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>有返回值的函数，将返回值赋值给变量，或直接使用返回值
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">    <span class="type">int</span> c = <span class="built_in">Add</span>(a, b);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, <span class="built_in">Add</span>(a, b));</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>参数要与变量类型对应，定义<code>int</code>则对应传入<code>int</code>，定义<code>double</code>则对应传入<code>double</code>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">int</span> <span class="title">Add</span><span class="params">(<span class="type">double</span> a, <span class="type">double</span> b)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="type">double</span> x, y;****</span><br><span class="line">    <span class="comment">// scanf ...</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%f\n&quot;</span>, <span class="built_in">Add</span>(x, y));</span><br><span class="line">    <span class="comment">// 错误示例：char x, y; Add(x, y); Add的参数是double，不能传char参数</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
<p><strong>函数内对参数的修改，不会影响到调用函数所传的变量</strong></p>
<p>尝试</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> <span class="title">Change</span><span class="params">(<span class="type">int</span> a)</span> </span>&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;Test1: %d\n&quot;</span>, a);</span><br><span class="line">    a += <span class="number">2</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;Test2: %d\n&quot;</span>, a);</span><br><span class="line">    a *= <span class="number">3</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;Test3: %d\n&quot;</span>, a);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="type">int</span> x = <span class="number">10</span>;</span><br><span class="line">    <span class="built_in">Change</span>(x);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;Test4: %d\n&quot;</span>, x);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出为</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Test1: 10</span><br><span class="line">Test2: 12</span><br><span class="line">Test3: 36</span><br><span class="line">Test4: 10</span><br></pre></td></tr></table></figure>
<p>可以这样认为，将<code>main</code>中的<code>x</code>传入<code>Change</code>函数时，函数得到的是<code>x</code>的一个副本<code>a</code>，对<code>a</code>的任何修改都是对副本的修改，不会影响<code>x</code>本来的值。</p>
<p>这里传给函数的<code>x</code>称为“实际参数”，函数得到的<code>a</code>称为“形式参数”。</p>
<p>在<code>C++</code>中，如果想让函数得到变量“本体”，可使用叫做“引用”的语法，即在参数前加“<code>&amp;</code>”符号：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> <span class="title">Change</span><span class="params">(<span class="type">int</span> &amp;a)</span> </span>&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;Test1: %d\n&quot;</span>, a);</span><br><span class="line">    a += <span class="number">2</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;Test2: %d\n&quot;</span>, a);</span><br><span class="line">    a *= <span class="number">3</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;Test3: %d\n&quot;</span>, a);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="type">int</span> x = <span class="number">10</span>;</span><br><span class="line">    <span class="built_in">Change</span>(x);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;Test4: %d\n&quot;</span>, x);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出为</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Test1: 10</span><br><span class="line">Test2: 12</span><br><span class="line">Test3: 36</span><br><span class="line">Test4: 36</span><br></pre></td></tr></table></figure>
<p>两份代码唯一区别是
<code>Change</code>函数定义时，其参数定义由<code>int a</code>改为了<code>int &amp;a</code>，表示对传入参数的引用，这样对<code>a</code>的修改会对本体<code>x</code>生效。</p>
<p><strong>函数的返回可以“截断”后续的逻辑</strong></p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">int</span> <span class="title">f</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(a &lt; b) <span class="keyword">return</span> a;</span><br><span class="line">    <span class="keyword">return</span> b;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, <span class="built_in">f</span>(<span class="number">3</span>, <span class="number">5</span>));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, <span class="built_in">f</span>(<span class="number">8</span>, <span class="number">5</span>));</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>当满足
<code>a&lt;b</code>时，<code>return a</code>的逻辑会被执行，函数中任何<code>return</code>被执行时，之后的代码都不会再被执行。</p>
<p>当不满足<code>a&lt;b</code>时，代码会继续向下执行到<code>return b</code>。</p>
<h3 id="函数的意义">函数的意义</h3>
<ul>
<li>复用同一段代码处理相同的任务</li>
<li>让程序更加模块化，更易于理解和维护</li>
</ul>
<p>例：分别求 <span class="math inline">\(1\sim 10\)</span>、 <span
class="math inline">\(200\sim 300\)</span>的和并输出</p>
<p>不使用函数：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="type">int</span> sum;</span><br><span class="line"></span><br><span class="line">    sum = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">1</span>; i &lt;= <span class="number">10</span>; i ++) &#123;</span><br><span class="line">        sum += i;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;1~10的和=%d&quot;</span>, sum);</span><br><span class="line"></span><br><span class="line">    sum = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">200</span>; i &lt;= <span class="number">300</span>; i ++) &#123;</span><br><span class="line">        sum += i;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;200~300的和=%d&quot;</span>, sum);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">使用函数：</span><br><span class="line"></span><br><span class="line">```<span class="function">cpp</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">CalSum</span><span class="params">(<span class="type">int</span> start, <span class="type">int</span> end)</span> </span>&#123;</span><br><span class="line">    <span class="type">int</span> sum = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i = start; i &lt;= end; i ++) &#123;</span><br><span class="line">        sum += i;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d~%d的和=%d&quot;</span>, start, end, sum);</span><br><span class="line">    <span class="keyword">return</span> sum;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="built_in">CalSum</span>(<span class="number">1</span>, <span class="number">10</span>);</span><br><span class="line">    <span class="built_in">CalSum</span>(<span class="number">200</span>, <span class="number">300</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>使用函数精简了代码，且逻辑清晰。</p>
<p>如果这时任务改成，“求一段数从第一个数开始，奇数位置上的和”，如果没有使用函数，则每个执行此任务的地方都要修改代码。当使用函数时，只需要修改<code>CalSum</code>这个函数内的<code>i ++</code>改成<code>i += 2</code>就可以，代码维护变得简单。</p>
<h2 id="函数的递归">函数的递归</h2>
<h3 id="递归的定义">递归的定义</h3>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">名词定义 递归() &#123;</span><br><span class="line">    <span class="keyword">return</span> 递归();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>递归即函数自己调用自己，或若干个函数在互相调用。当然上面的示例看起来永远不会结束，它是一个无限递归。</p>
<p>通常用递归来解决可以被分解的问题，在问题足够小的时候有一个终点。比如给出
<span class="math inline">\(n\)</span>，求 <span
class="math inline">\(n\)</span>的阶乘，即 <span
class="math inline">\(n!=1\times 2\times, \dots,
n\)</span>，递归可以这样写：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">int</span> <span class="title">f</span><span class="params">(<span class="type">int</span> x)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(x == <span class="number">0</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">f</span>(x - <span class="number">1</span>) * x;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>所以一个合理的递归，应该有两部分组成</p>
<ol type="1">
<li>对问题的分解：调用函数本身去解决一个“更小”的问题
<ul>
<li>这里<code>f(x - 1)</code>
就是对问题的分解，递归地去处理规模更小的<code>x-1</code>的阶乘，并乘以<code>x</code>得到<code>f(x)</code>的解</li>
</ul></li>
<li>递归的终点：当问题足够小时，以一个确定的逻辑返回，而不再继续分解
<ul>
<li><code>if(x == 0) return 1</code>就是在做这件事，确保递归不会无限进行下去，<code>0</code>是一个足够小的问题</li>
</ul></li>
</ol>
<h3 id="递归的意义">递归的意义</h3>
<p>实际上，任何递归逻辑都可以用非递归的代码去完成，比如阶乘，用一个循环将
<span class="math inline">\(1\sim n\)</span>累乘起来也能实现。</p>
<p>但在一些复杂问题中，递归能帮助我们更好地理清思路，将问题抽丝剥茧，要比从全局思考正确步骤更为容易。</p>
<p>以汉诺塔问题为例，有三根柱子，一根柱子上有 <span
class="math inline">\(n\)</span>个大小不同的盘子，将所有的盘子移动到另一根柱子上，在移动过程中，任何时候都不能让大盘子在小盘子上面。</p>
<img src="/2025-03-06-04-%E5%87%BD%E6%95%B0%E4%B8%8E%E7%BB%93%E6%9E%84%E4%BD%93/hanoi.gif" class="" title="三个盘子的汉诺塔">
<p>如果把柱子从左到右命名为 <span class="math inline">\(A\)</span>、
<span class="math inline">\(B\)</span>、 <span
class="math inline">\(C\)</span>，以“<code>A to C</code>”这样的指令表示把
<span class="math inline">\(A\)</span>最顶部的盘子挪到 <span
class="math inline">\(C\)</span>的最顶部，当一开始 <span
class="math inline">\(A\)</span>柱有 <span
class="math inline">\(n\)</span>个盘子，以一系列指令给出将所有盘子在约束条件下从
<span class="math inline">\(A\)</span>移到 <span
class="math inline">\(C\)</span>的方案。</p>
<p>不用递归的话，面对这个问题还是很懵的，那么定义一个这样的函数：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> <span class="title">Hanoi</span><span class="params">(<span class="type">int</span> n, <span class="type">char</span> x, <span class="type">char</span> y, <span class="type">char</span> z)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// TODO</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>表示把<code>n</code>个盘子从变量<code>x</code>所表示的柱子，利用<code>y</code>作为辅助柱子，以符合题目要求的方式全部移动到<code>z</code>所表示的柱子上。</p>
<p>那么第一次调用这个函数应该是这样：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Hanoi</span>(n, <span class="string">&#x27;A&#x27;</span>, <span class="string">&#x27;B&#x27;</span>, <span class="string">&#x27;C&#x27;</span>);</span><br></pre></td></tr></table></figure>
<p>可以这样分解该问题：</p>
<ol type="1">
<li><span class="math inline">\(A\)</span>上的 <span
class="math inline">\(n-1\)</span>个盘子通过 <span
class="math inline">\(C\)</span>先移到 <span
class="math inline">\(B\)</span></li>
<li><span class="math inline">\(A\)</span>上最底部的那个盘子直接移到
<span class="math inline">\(C\)</span>，即输出“<code>A to C</code>”</li>
<li><span class="math inline">\(B\)</span>上当前的 <span
class="math inline">\(n-1\)</span>个盘子通过 <span
class="math inline">\(A\)</span>移到 <span
class="math inline">\(C\)</span>， <span
class="math inline">\(C\)</span>上在第2步放的那个最大的盘子完全不影响这
<span class="math inline">\(n-1\)</span>个盘子的任何移动</li>
</ol>
<p>这3步就完成了整个任务。而第1步和第3步是相同的问题，只是问题规模减小了1，且起点柱子和终点柱子不同了而已，他们可以递归地调用<code>Hanoi</code></p>
<p>完整的递归代码这样实现：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> <span class="title">Hanoi</span><span class="params">(<span class="type">int</span> n, <span class="type">int</span> x, <span class="type">int</span> y, <span class="type">int</span> z)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(n == <span class="number">1</span>) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%c to %c\n&quot;</span>, x, z); <span class="comment">// x 和 z 具体是哪个柱子这里并不重要</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="built_in">Hanoi</span>(n - <span class="number">1</span>, x, z, y);      <span class="comment">// 执行第 1 步</span></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%c to %c\n&quot;</span>, x, z); <span class="comment">// 执行第 2 步</span></span><br><span class="line">        <span class="built_in">Hanoi</span>(n - <span class="number">1</span>, y, x, z);      <span class="comment">// 执行第 3 步</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="type">int</span> n = <span class="number">3</span>;</span><br><span class="line">    <span class="built_in">Hanoi</span>(n, <span class="string">&#x27;A&#x27;</span>, <span class="string">&#x27;B&#x27;</span>, <span class="string">&#x27;C&#x27;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="递归栈">递归栈</h3>
<p>当一个函数被调用或被递归地调用，调用结束时代码还要继续往下进行，操作系统怎么“恢复现场”呢？</p>
<p>比如</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">int</span> <span class="title">f</span><span class="params">(<span class="type">int</span> x)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> x == <span class="number">0</span> ? <span class="number">1</span> : <span class="built_in">f</span>(x - <span class="number">1</span>) * x;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="type">int</span> n = <span class="number">10</span>, a = <span class="number">5</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, <span class="built_in">f</span>(n));</span><br><span class="line">    <span class="comment">// 当刚刚的 f(n) 运行结束，操作系统如何回到这里，并知道 n==10，a==5？</span></span><br><span class="line">    n ++;</span><br><span class="line">    a += n;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d %d\n&quot;</span>, n, a);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这就有一个“栈内存”的概念，每当调用函数，在进入函数执行前，系统会“保护现场”，把所有信息保存到一块内存中。当函数执行完毕，会从这块栈内存把所有信息读回来“恢复现场”并继续执行接下来的代码。</p>
<p>通常系统的栈内存有一个固定大小，当递归时，每次调用都会“保护现场”，占用一些栈内存，那么递归得太深，就可能把栈内存用尽，程序就会出错，这种情况称为“栈溢出”。</p>
<p>比如递归求一个很大的阶乘</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">f</span><span class="params">(<span class="type">int</span> n)</span> </span>&#123;</span><br><span class="line">    <span class="type">int</span> ret;</span><br><span class="line">    ret = n == <span class="number">0</span> ? <span class="number">1</span> : <span class="built_in">f</span>(n - <span class="number">1</span>) * n;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;now: %d\n&quot;</span>, n);</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="type">int</span> n;</span><br><span class="line">    n = <span class="number">1000000000</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, <span class="built_in">f</span>(n));</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>因为现代编译器会做一些优化来尽可能避免栈溢出，这里作为栈溢出的例子增加了一些无用代码来确保溢出不会被优化掉，这方面的知识大家以后会了解。</p>
<p>栈溢出的知识点告诉我们，在思考一个问题解法的时候，如果需要的递归深度离谱的高，就要慎重考虑一下是不是最好的方案了。</p>
<p>当然还有一个更常见的情况，也是建议，较大的数组放在<code>main</code>函数外面</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 推荐做法：</span></span><br><span class="line"><span class="type">int</span> a[<span class="number">10000</span>];</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 不建议做法：</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="type">int</span> a[<span class="number">10000</span>];</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>函数内的一切变量都开在“栈内存”中，过大会导致栈溢出。而函数外的变量会开在“堆内存”中，这就是其他话题了，此处不再赘述。</p>
<h2 id="结构体">结构体</h2>
<h3 id="结构体的概念">结构体的概念</h3>
<p>将多种变量封装到一起从而自定义一种类型。</p>
<p>一个“学生”有学号、年龄、身高、体重等信息，如果一个程序想以“学生”作为对象来做数据处理，或者以某种条件筛选学生并返回学生信息：</p>
<p>不使用结构体</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 输出学生信息</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">PrintStu</span><span class="params">(<span class="type">int</span> ID, <span class="type">int</span> age, <span class="type">int</span> height, <span class="type">int</span> weight)</span> </span>&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d %d %d %d\n&quot;</span>, ID, age, height, weight);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 获取特定ID的学生信息并输出</span></span><br><span class="line"><span class="type">int</span> ID_list[<span class="number">1000</span>], age_list[<span class="number">1000</span>], height_list[<span class="number">1000</span>], weight_list[<span class="number">1000</span>];</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">Search</span><span class="params">(<span class="type">int</span> ID_search)</span> </span>&#123;</span><br><span class="line">    <span class="type">int</span> id_i = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(; id_i &lt; <span class="number">1000</span>; id_i ++) &#123;</span><br><span class="line">        <span class="keyword">if</span>(ID_list[id_i] == ID_search) &#123;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">PrintStu</span>(ID_list[id_i], age_list[id_i], height_list[id_i], weight_list[id_i]);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这还只是简单的需求。如果一个学生要记录更多维度的信息如姓名、住址、手机号等等等等，代码的编写将十分繁琐且不便维护。</p>
<p>将一个学生作为一个统一的对象，把信息打包封装：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Student</span>&#123;</span><br><span class="line">    <span class="type">int</span> ID, age, height, weight;</span><br><span class="line">&#125;;</span><br><span class="line">Student s[<span class="number">1000</span>];</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">Search</span><span class="params">(<span class="type">int</span> ID_search)</span> </span>&#123;</span><br><span class="line">    <span class="type">int</span> id_i = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(; id_i &lt; <span class="number">1000</span>; id_i ++) &#123;</span><br><span class="line">        <span class="keyword">if</span>(s[id_i].ID == ID_search) &#123;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">PrintStu</span>(s[id_i].ID, s[id_i].age, s[id_i].height, s[id_i].weight);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这样<code>Student</code>
就可以作为一个自定义的数据类型，去定义变量或数组，每个该类型的变量都会包含“学生”这一概念的各种信息。</p>
<p>用结构体定义的变量称为该结构体的“对象”，用“<code>.</code>”来调用对象内的变量。</p>
<h3 id="成员函数">成员函数</h3>
<p>如果一些功能只与结构体自身有关，可以理解为这个结构体自身的某种功能，那么可以将函数定义为结构体的“成员函数”，同样用“<code>.</code>”来调用对象内的成员函数，成员函数可以直接使用和修改对象内的变量。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Student</span>&#123;</span><br><span class="line">    <span class="type">int</span> ID, age, height, weight;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">Init</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        ID = age = height = weight = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">Print</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d %d %d %d\n&quot;</span>, ID, age, height, weight);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line">Student s[<span class="number">1000</span>];</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">Search</span><span class="params">(<span class="type">int</span> ID_search)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">    s[id_i].<span class="built_in">Print</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="运算符的重载">运算符的重载</h3>
<p><code>int</code>、<code>double</code>这些类型天然具备的
<code>+,-,*,/,&gt;,&lt;</code>
等操作，作为自定义变量类型的结构体并没有良好的定义。</p>
<p>我们当然可以用函数实现结构体之间的各种运算，但如果能用这些运算符，会让代码逻辑更为清晰。</p>
<p>比如两个学生的大小用年龄的大小关系来定义，函数可以这样实现：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Student</span>&#123;</span><br><span class="line">    <span class="type">int</span> ID, age, height, weight;</span><br><span class="line">    <span class="function"><span class="type">bool</span> <span class="title">Bigger</span><span class="params">(<span class="type">const</span> Student &amp;that)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 当前对象比 that 对象的 age 更大则返回 true</span></span><br><span class="line">        <span class="keyword">return</span> age &gt; that.age;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line">Student a, b;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    a.age = <span class="number">10</span>;</span><br><span class="line">    b.age = <span class="number">9</span>;</span><br><span class="line">    <span class="keyword">if</span>(a.<span class="built_in">Bigger</span>(b)) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;a is bigger\n&quot;</span>);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;a is not bigger\n&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>而重载运算符，就可以在结构体的对象之间执行相应的运算，比如</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Student</span>&#123;</span><br><span class="line">    <span class="type">int</span> ID, age, height, weight;</span><br><span class="line">    <span class="type">bool</span> <span class="keyword">operator</span>&gt;(<span class="type">const</span> Student &amp;that) &#123;</span><br><span class="line">        <span class="comment">// 当前对象比 that 对象的 age 更大则返回 true</span></span><br><span class="line">        <span class="keyword">return</span> age &gt; that.age;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line">Student a, b;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    a.age = <span class="number">10</span>;</span><br><span class="line">    b.age = <span class="number">9</span>;</span><br><span class="line">    <span class="keyword">if</span>(a &gt; b) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;a is bigger\n&quot;</span>);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;a is not bigger\n&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/ACM/" rel="tag"># ACM</a>
              <a href="/tags/Algorithm/" rel="tag"># Algorithm</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2025-03-06-03-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%BE%AA%E7%8E%AF%E7%BB%93%E6%9E%84/" rel="prev" title="03.数组与循环结构">
                  <i class="fa fa-angle-left"></i> 03.数组与循环结构
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2025-03-06-05-%E7%BA%BF%E6%80%A7%E8%A1%A8/" rel="next" title="05.线性表">
                  05.线性表 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments utterances-container"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">CSGrandeur</span>
  </div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://fastly.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://fastly.jsdelivr.net/npm/@next-theme/pjax@0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/sidebar.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://fastly.jsdelivr.net/npm/hexo-generator-searchdb@1.4.1/dist/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>







  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://fastly.jsdelivr.net/npm/mathjax@3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


<script class="next-config" data-name="utterances" type="application/json">{"enable":true,"repo":"CSGrandeur/csgrandeur.github.io","issue_term":"pathname","theme":"github-light"}</script>
<script src="/js/third-party/comments/utterances.js"></script>

</body>
</html>
